package com.ruoyi.weixin.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.weixin.mapper.AddressMapper;
import com.ruoyi.weixin.domain.Address;
import com.ruoyi.weixin.service.IAddressService;
import com.ruoyi.common.core.text.Convert;

/**
 * 收件地址Service业务层处理
 * 
 * @author cloudoer
 * @date 2019-09-09
 */
@Service
public class AddressServiceImpl implements IAddressService 
{
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 查询收件地址
     * 
     * @param addressId 收件地址ID
     * @return 收件地址
     */
    @Override
    public Address selectAddressById(String addressId)
    {
        return addressMapper.selectAddressById(addressId);
    }

    @Override
    public List<Address> selectAddressByClientId(String clientId) {
        return addressMapper.selectAddressByClientId(clientId);
    }

    /**
     * 查询收件地址列表
     * 
     * @param address 收件地址
     * @return 收件地址
     */
    @Override
    public List<Address> selectAddressList(Address address)
    {
        return addressMapper.selectAddressList(address);
    }

    /**
     * 新增收件地址
     * 
     * @param address 收件地址
     * @return 结果
     */
    @Override
    public int insertAddress(Address address)
    {
        address.setAddressId(UUID.randomUUID().toString().replace("-",""));
        if ("1".equals(address.getIsDefault())){
            clearDefault(address.getClientId());
        }
        return addressMapper.insertAddress(address);
    }

    /**
     * 修改收件地址
     * 
     * @param address 收件地址
     * @return 结果
     */
    @Override
    public int updateAddress(Address address)
    {
        if ("1".equals(address.getIsDefault())){
            clearDefault(address.getClientId());
        }
        return addressMapper.updateAddress(address);
    }

    @Override
    public boolean addressDefault(String clientId, String addressId) {
        /*Address address = new Address();
        address.setClientId(clientId);
        address.setIsDefault("1");
        List<Address> addresses = addressMapper.selectAddressList(address);
        if (addresses != null && addresses.size() > 0){
            for (Address a : addresses){
                a.setIsDefault("0");
                addressMapper.updateAddress(a);
            }
        }*/
        clearDefault(clientId);
        Address address1 = new Address();
        address1.setIsDefault("1");
        address1.setAddressId(addressId);
        addressMapper.updateAddress(address1);
        return true;
    }

    /**
     * 清除默认的标识
     * @param clientId
     */
    public void clearDefault(String clientId){
        Address address = new Address();
        address.setClientId(clientId);
        address.setIsDefault("1");
        List<Address> addresses = addressMapper.selectAddressList(address);
        if (addresses != null && addresses.size() > 0){
            for (Address a : addresses){
                a.setIsDefault("0");
                addressMapper.updateAddress(a);
            }
        }
    }

    /**
     * 删除收件地址对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAddressByIds(String ids)
    {
        return addressMapper.deleteAddressByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除收件地址信息
     * 
     * @param addressId 收件地址ID
     * @return 结果
     */
    public int deleteAddressById(String addressId)
    {
        return addressMapper.deleteAddressById(addressId);
    }
}
